package com.hrms.domain;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import javax.servlet.http.HttpServletRequest;

import com.google.appengine.api.datastore.Key;
import com.hrms.core.Utils;

@PersistenceCapable(detachable="true")
public class Task extends BusinessObjectImpl {
	
	public static final String TYPE = Task.class.getSimpleName();

	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key key;
	
	@Persistent
	private String idUrl; // represents bug number
	@Persistent
	private String userId;
	@Persistent
	private String type; //BUG,ENH,DOC, OTHR
	@Persistent
	private String description;
	
	@Persistent
	private Date date;//plan by
	
	@Persistent
	private String status;  //Blocking, Planned, InProgress, Completed
	
	/*@Persistent(mappedBy = "task", defaultFetchGroup = "true")
	private List<BlockingStep> blockingSteps;*/
	
	public Task(Map<String, String[]> parameterMap) throws IllegalArgumentException, IllegalAccessException, ParseException {
		super(parameterMap);
	}

	public Task() {
		super();
	}

	public Task(Map<String, String[]> parameterMap, String prefix)
			throws IllegalArgumentException, IllegalAccessException,
			ParseException {
		super(parameterMap, prefix);
	}

	

	@Override
	protected void performPostPersistEvents(String userAction,
			HttpServletRequest request) throws Exception {
		
		TaskStatusLog tsl = new TaskStatusLog();
		tsl.setStatus(getStatus());
		tsl.setTaskKey(getKey());
		tsl.save();

		super.performPostPersistEvents(userAction, request);
	}

	@Override
	public void jdoPreStore()  {
		if(getDate() == null){
			setDate(Utils.getTodayDate());
		}
			setUserId(userId.toLowerCase());	
	}

	public List<DailyCell> getDailyCells() {
		return select(DailyCell.class, "where taskKey == :p0", new Object[]{ getKey() });
	}

	public List<BlockingStep> getBlockingSteps() {
		return select(BlockingStep.class, "where taskKey == :p0", new Object[]{ getKey() });
	}

	public Key getKey() {
		return key;
	}
	public void setKey(Key key) {
		this.key = key;
	}

	public String getIdUrl() {
		return idUrl;
	}

	public void setIdUrl(String idUrl) {
		this.idUrl = idUrl;
	}

	public String getUserId() {
		return userId;
	}
	public void setUserId(String userId) {
		this.userId = userId;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public Date getDate() {
		return date;
	}
	public void setDate(Date date) {
		this.date = date;
	}
	public String getStatus() {
		return status;
	}
	public void setStatus(String status) {
		this.status = status;
	}
		
}
